package com.demo.netty;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.*;
import org.jboss.netty.channel.local.LocalAddress;
import org.jboss.netty.util.Timeout;
import org.jboss.netty.util.Timer;
import org.jboss.netty.util.TimerTask;

import java.io.IOException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

/**
 * @author jasper
 */
public class ClientHandler extends SimpleChannelUpstreamHandler {
    private ClientBootstrap bootstrap;
    private final Timer timer;

    public ClientHandler(ClientBootstrap bootstrap, Timer timer) {
        this.bootstrap = bootstrap;
        this.timer = timer;
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        MessageType message = (MessageType) e.getMessage();
        Channel channel = e.getChannel();
        System.out.println("client >");
        switch (message.getType()) {
            case MessageType.TWO:
                System.out.println("receive a  message: " + message.getContext());
                break;
            default:
                throw new IOException("unknown type message " + message.getType());
        }

        System.out.println("向server发送消息：> ");
        Scanner scanner = new Scanner(System.in);
        MessageType messageType = new MessageType();
        String next = scanner.next().trim();
        String[] split = next.split(",");
        messageType.setType(Integer.parseInt(split[0]));
        messageType.setContext(split[1]);
        channel.write(messageType);
    }

    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        super.channelConnected(ctx, e);
        Channel channel = ctx.getChannel();


//        MessageType message = new MessageType();
//        message.setContext("hello server ,i am client");
//        message.setType(3);
//        channel.write(message);

        System.out.println("client channel connect");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {

//        System.out.println(""+e.getCause().getMessage());

        if (e.getCause() instanceof ConnectException) {
            System.out.println("连接异常");
            e.getChannel().close();
        }
    }

    @Override
    public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        System.out.println("c channelDisconnected");
        super.channelDisconnected(ctx, e);
    }

    /**
     * 服务器重连
     * @param ctx
     * @param e
     * @throws Exception
     */
    @Override
    public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        System.out.println("c channelClosed");
        final String host = "127.0.0.1";
        final int port = 10081;
        System.out.println("clientHandler ClientBootstrap = "+bootstrap);
        ChannelFuture connect = bootstrap.connect(new LocalAddress(port));
        Channel channel = connect.getChannel();
        MessageType messageType = new MessageType(MessageType.THREE,"reconnect");
        channel.write(messageType);
        System.out.println("重新连接服务器.....");
    }
}
